package com.sdkj.recruit.util;


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 接口帮助类 查询一年内发票 1、发票查验
 */
public class FpcyHttpUtil2 {

	public final static int timeout = 25000;

	public static String doHttpOutput(String url, String outputStr) {
		String result = "";
		InputStream is = null;
		int len = 0;
		int tmp = 0;
		OutputStream output = null;
		BufferedOutputStream objOutput = null;
		String charSet = "gbk";
		HttpURLConnection con = null;
		BufferedReader br = null;
		try {
			con = getConnection(url);
			output = con.getOutputStream();
			objOutput = new BufferedOutputStream(output);
			objOutput.write(outputStr.getBytes(charSet));
			objOutput.flush();
			objOutput.close();
			output.close();
			int responseCode = con.getResponseCode();
			if (responseCode == 200) {
				is = con.getInputStream();
				int dataLen = is.available();
				br = new BufferedReader(new InputStreamReader(is, charSet));
				byte[] bytes = new byte[dataLen];
				while ((tmp = is.read()) != -1) {
					bytes[len++] = (byte) tmp;
					if (len == dataLen) {
						dataLen = bytes.length + dataLen;
						byte[] newbytes = new byte[dataLen];
						for (int i = 0; i < bytes.length; i++) {
							newbytes[i] = bytes[i];
						}
						bytes = newbytes;
					}
				}
				result = new String(bytes, 0, len, "gbk");
			}
		} catch (Exception e2) {
			e2.printStackTrace();
		} finally {
			try {
				if (objOutput != null)
					objOutput.close();
				if (output != null)
					output.close();
				if (br != null)
					br.close();
				if (con != null)
					con.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	public static String doHttpOutfile(String url, String outputStr) {
		String result = "";
		InputStream is = null;
		int len = 0;
		int tmp = 0;
		OutputStream output = null;
		BufferedOutputStream objOutput = null;
		String charSet = "gbk";
		HttpURLConnection con = null;
		BufferedReader br = null;
		try {
			con = getConnection(url);
			output = con.getOutputStream();
			objOutput = new BufferedOutputStream(output);
			objOutput.write(outputStr.getBytes(charSet));
			objOutput.flush();
			objOutput.close();
			output.close();
			int responseCode = con.getResponseCode();
			if (responseCode == 200) {
				is = con.getInputStream();
				int dataLen = is.available();
				br = new BufferedReader(new InputStreamReader(is, charSet));
				byte[] bytes = new byte[dataLen];
				while ((tmp = is.read()) != -1) {
					bytes[len++] = (byte) tmp;
					if (len == dataLen) {
						dataLen = bytes.length + dataLen;
						byte[] newbytes = new byte[dataLen];
						for (int i = 0; i < bytes.length; i++) {
							newbytes[i] = bytes[i];
						}
						bytes = newbytes;
					}
				}
				String filename = con.getHeaderField("filename");
				FileOutputStream fos = new FileOutputStream(filename);
				fos.write(bytes);
				fos.flush();
				// 关闭此文件输出流并释放与此流有关的所有系统资源。此文件输出流不能再用于写入字节。
				// 如果此流有一个与之关联的通道，则关闭该通道。
				fos.close();

			}
		} catch (Exception e2) {
			e2.printStackTrace();
		} finally {
			try {
				if (objOutput != null)
					objOutput.close();
				if (output != null)
					output.close();
				if (br != null)
					br.close();
				if (con != null)
					con.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	private static HttpURLConnection getConnection(String URL) throws Exception {
		HttpURLConnection con = null;
		con = (HttpURLConnection) new URL(URL).openConnection();
		con.setRequestMethod("POST");
		con.setConnectTimeout(timeout);
		con.setReadTimeout(timeout);
		con.setUseCaches(false);
		con.setDoInput(true);
		con.setDoOutput(true);
		return con;

	}

	/**
	 * 发票查验 接口 url 客户端通信地址 （1）增值税专用发票（01）：发票代码、发票号码、开票日期和开具金额(不含税)；
	 * （2）机动车销售统一发票（03）：发票代码、发票号码、开票日期和不含税价；
	 * （3）增值税普通发票（04）、增值税电子普通发票（10）、增值税普通发票(卷票)（11）： 发票代码、发票号码、开票日期和校验码后6位；
	 * （4）货物运输业增值税专用发票（02）：发票代码、发票号码、开票日期和合计金额。
	 */
	public static String fpcy(String url, String fpdm, String fphm,
			String kprq, String value) {
		try {
			long ll = System.currentTimeMillis();
			String xml = "<cd><sv>cd</sv><fpdm>" + fpdm + "</fpdm><fphm>"
					+ fphm + "</fphm><kprq>" + kprq + "</kprq><value>" + value
					+ "</value></cd>";
			System.out.println("send msg:\n" + xml);
			String rResult = FpcyHttpUtil2.doHttpOutput(url, xml);
			System.out.println("发票" + fpdm + " , " + fphm + " : " + rResult);
			System.out.println("发票" + fpdm + " , " + fphm + " 查验时间:"
					+ (System.currentTimeMillis() - ll) + "ms  ");
			return rResult;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	// 调用方式
	public static void main(String[] args) {
		FpcyHttpUtil2.fpcy("http://103.235.245.98:8805", "044001700211","10282346", "20180920", "064869");
		//FpcyHttpUtil2.fpcy("http://103.235.245.98:8805", "037001700112","07155157", "2018-01-10", "915339");

	}
}